package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.SysCity;
import com.yunkeji.dao.mapper.SysCityMapper;
import com.yunkeji.dao.vo.SysCityVo;
import com.yunkeji.system.domain.bo.SysCityBo;
import com.yunkeji.system.service.ISysCityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 城市信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class SysCityServiceImpl implements ISysCityService {

    private final SysCityMapper baseMapper;

    /** 查询城市信息 */
    @Override
    public SysCityVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询城市信息列表 */
    @Override
    public TableDataInfo<SysCityVo> queryPageList(SysCityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysCity> lqw = buildQueryWrapper(bo);
        Page<SysCityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询城市信息列表 */
    @Override
    public List<SysCityVo> queryList(SysCityBo bo) {
        LambdaQueryWrapper<SysCity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysCity> buildQueryWrapper(SysCityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCity> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPid() != null, SysCity::getPid, bo.getPid());
        lqw.like(
                StringUtils.isNotBlank(bo.getShortname()),
                SysCity::getShortname,
                bo.getShortname());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysCity::getName, bo.getName());
        lqw.like(
                StringUtils.isNotBlank(bo.getMergerName()),
                SysCity::getMergerName,
                bo.getMergerName());
        lqw.eq(bo.getLevel() != null, SysCity::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getPinyin()), SysCity::getPinyin, bo.getPinyin());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SysCity::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getZipCode()), SysCity::getZipCode, bo.getZipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFirst()), SysCity::getFirst, bo.getFirst());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), SysCity::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), SysCity::getLat, bo.getLat());
        return lqw;
    }

    /** 新增城市信息 */
    @Override
    public Boolean insertByBo(SysCityBo bo) {
        SysCity add = BeanUtil.toBean(bo, SysCity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改城市信息 */
    @Override
    public Boolean updateByBo(SysCityBo bo) {
        SysCity update = BeanUtil.toBean(bo, SysCity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(SysCity entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除城市信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
